﻿using Ekton_Manage_DomainAbstractions;
using Ekton_Manage_Framework.Methods;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Ekton_Manage_InfrastructureCore.SqlSugarHelper
{
    public class SqlSugarMethod
    {
        private readonly SqlSugarInstance _instance;
        public static SqlSugarClient client = null;
        private readonly HttpGetUser _httpGetUser;
        public SqlSugarMethod(SqlSugarInstance instance,
                              HttpGetUser httpGetUser)
        {
            _instance = instance;
            client = _instance.GetInstance();
            _httpGetUser = httpGetUser;
        }

        public async Task<bool> InsertData<T>(T data) where T : Entity<int>, new()
        {
            dynamic user = await _httpGetUser.GetUser();
            data.create_op_date = DateTime.Now;
            data.create_op_id = user == null ? 0 : user.id;
            data.create_op_name = user == null ? "" : user.name;
            return client.Insertable(data).ExecuteCommand() > 0;
        }

        public async Task<List<T>> QueryData<T>(string sql) where T : class, new()
        {
            return client.SqlQueryable<T>(sql).ToList();
        }
    }
}
